package com.itita.ww2.puredb.model;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;
import org.joda.time.DateTime;
import org.joda.time.Days;

import com.google.common.base.Splitter;
import com.itita.ww2.puredb.DAO.UserTimeLimitedPayLogMapper;
import com.itita.ww2.utils.MyBatisSessionUtil;
import com.itita.ww2.utils.TimeUtil;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;

public class UserTimeLimitedPayLog extends UserTimeLimitedPayLogKey {

	/**
	 * This field was generated by MyBatis Generator. This field corresponds to the database column user_timelimitedpaylog.receiveDays
	 * @mbg.generated
	 */
	private String receiveDays;
	/**
	 * This field was generated by MyBatis Generator. This field corresponds to the database column user_timelimitedpaylog.buyTime
	 * @mbg.generated
	 */
	private long buyTime;
	/**
	 * This field was generated by MyBatis Generator. This field corresponds to the database column user_timelimitedpaylog.expire
	 * @mbg.generated
	 */
	private boolean expire;

	/**
	 * This method was generated by MyBatis Generator. This method returns the value of the database column user_timelimitedpaylog.receiveDays
	 * @return  the value of user_timelimitedpaylog.receiveDays
	 * @mbg.generated
	 */
	public String getReceiveDays() {
		return receiveDays;
	}

	/**
	 * This method was generated by MyBatis Generator. This method sets the value of the database column user_timelimitedpaylog.receiveDays
	 * @param receiveDays  the value for user_timelimitedpaylog.receiveDays
	 * @mbg.generated
	 */
	public void setReceiveDays(String receiveDays) {
		this.receiveDays = receiveDays;
	}

	/**
	 * This method was generated by MyBatis Generator. This method returns the value of the database column user_timelimitedpaylog.buyTime
	 * @return  the value of user_timelimitedpaylog.buyTime
	 * @mbg.generated
	 */
	public long getBuyTime() {
		return buyTime;
	}

	/**
	 * This method was generated by MyBatis Generator. This method sets the value of the database column user_timelimitedpaylog.buyTime
	 * @param buyTime  the value for user_timelimitedpaylog.buyTime
	 * @mbg.generated
	 */
	public void setBuyTime(long buyTime) {
		this.buyTime = buyTime;
	}

	/**
	 * This method was generated by MyBatis Generator. This method returns the value of the database column user_timelimitedpaylog.expire
	 * @return  the value of user_timelimitedpaylog.expire
	 * @mbg.generated
	 */
	public boolean isExpire() {
		return expire;
	}

	/**
	 * This method was generated by MyBatis Generator. This method sets the value of the database column user_timelimitedpaylog.expire
	 * @param expire  the value for user_timelimitedpaylog.expire
	 * @mbg.generated
	 */
	public void setExpire(boolean expire) {
		this.expire = expire;
	}

	public ISFSObject toSFSObject() {
		ISFSObject ref = SFSObject.newInstance();
		// 今日是否已领取过
		ref.putUtfString("sku", this.getSKU());
		ref.putBool("expire", expire);
		if (!expire) {
			ref.putBool("received", isReceivedToday());
		}
		return ref;
	}

	/**
	 * 获取购买天数(包含购买日)
	 */
	public int getDays() {
		return Math.abs(Days.daysBetween(new DateTime(buyTime), TimeUtil.getUtcNow()).getDays() + 1);
	}

	public List<String> getReceiveDaysToList() {
		return Splitter.on('|').splitToList(this.receiveDays);
	}

	public void addReceiveDays(int day) {
		if (StringUtils.isBlank(this.receiveDays)) {
			receiveDays = String.valueOf(day);
		} else {
			receiveDays += "|" + day;
		}
	}

	public boolean isReceivedToday() {
		return getReceiveDaysToList().contains(String.valueOf(getDays()));
	}

	/**
	 * 检查过期状态
	 * @param days 过期时长 (天)
	 * @param remainDays 保留时长,续费用 (天)
	 * @return false->已经完全过期,无法续费, true还未完全过期
	 */
	public boolean checkExpire(int days, int remainDays) {
		int realDays = getDays();
		if (realDays > days) {
			if (realDays <= days + remainDays) {
				// 过期保留remainDays
				if (!expire) {
					this.expire = true;
					update();
				}
			} else {
				delete();
				return false;
			}
		}
		return true;
	}

	public void update() {
		SqlSession session = MyBatisSessionUtil.getInstance().getSession();
		try {
			session.getMapper(UserTimeLimitedPayLogMapper.class).updateByPrimaryKey(this);
			session.commit();
		} catch (Exception e) {
			session.rollback();
		} finally {
			session.close();
		}
	}

	public void delete() {
		SqlSession session = MyBatisSessionUtil.getInstance().getSession();
		try {
			session.getMapper(UserTimeLimitedPayLogMapper.class).deleteByPrimaryKey(this);
			session.commit();
		} catch (Exception e) {
			session.rollback();
		} finally {
			session.close();
		}
	}

	public void insert() {
		SqlSession session = MyBatisSessionUtil.getInstance().getSession();
		try {
			session.getMapper(UserTimeLimitedPayLogMapper.class).insert(this);
			session.commit();
		} catch (Exception e) {
			session.rollback();
		} finally {
			session.close();
		}
	}

	public static UserTimeLimitedPayLog getByPrimaryKey(int uid, String sku) {
		SqlSession session = MyBatisSessionUtil.getInstance().getSession();
		UserTimeLimitedPayLog ref = null;
		try {
			ref = session.getMapper(UserTimeLimitedPayLogMapper.class)
					.selectByPrimaryKey(new UserTimeLimitedPayLogKey(uid, sku));
		} finally {
			session.close();
		}
		return ref;
	}
}